package org.example.controller;


import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.R;
import org.example.interceptor.UserHolder;
import org.example.pojo.entity.ApConcern;
import org.example.service.IApConcernService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-07
 */
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/front")
public class ApConcernController {

    @Autowired
    private IApConcernService apConcernService;

    /**
     * 是否关注
     */
    @GetMapping("/isconcern")
    public R isconcern(@RequestParam int concernUserId, HttpServletRequest request) {
        if (StringUtils.isEmpty(request.getHeader("token"))) {
            return R.ok(0);
        }
        QueryWrapper<ApConcern> wrapper = new QueryWrapper<>();
        wrapper.eq("concern_user_id", concernUserId);
        wrapper.eq("user_id", UserHolder.getUserId());
        if (apConcernService.getOne(wrapper) != null) {
            return R.ok(1);
        }
        return R.ok(0);
    }


    /**
     * 关注
     */
    @GetMapping("/concern")
    public R concern(@RequestParam int concernUserId) {
        Long count = apConcernService.saveConcern(concernUserId);
        return R.ok(count);
    }

    /**
     * 取消关注
     */
    @GetMapping("/unconcern")
    public R unconcern(@RequestParam int concernUserId) {
        Long count = apConcernService.unConcern(concernUserId);
        return R.ok(count);
    }
}
